package edu.mju.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.mju.commonutils.ReturnCode;
import edu.mju.eduservice.entity.StudyCourse;
import edu.mju.eduservice.entity.StudyTeacher;
import edu.mju.eduservice.service.StudyCourseService;
import edu.mju.eduservice.vo.CourseInfoVo;
import edu.mju.eduservice.vo.SearchCourse;
import edu.mju.eduservice.vo.SearchTeacher;
import edu.mju.eduservice.vo.chapter.CourseIssueVo;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author xiaoxin
 * @since 2020-08-31
 */
@RestController
@RequestMapping("/eduservice/study-course")
public class StudyCourseController {

    @Autowired
    private StudyCourseService studyCourseService;

    /**
     * 添加课程基本信息
     */
    @PostMapping("/addCourseInfo")
    public ReturnCode addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        //返回添加后的课程id
        String id = studyCourseService.saveCourseInfo(courseInfoVo);
        Map<String,Object> map = new HashMap<>();
        map.put("cId",id);
        return ReturnCode.success().data(map);
    }


    /**
     * 根据课程id查询课程基本信息
     * @param courseId 课程id
     * @return
     */
    @GetMapping("/getCourseInfo/{courseId}")
    public ReturnCode getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = studyCourseService.getCourseInfo(courseId);
        Map<String,Object> map = new HashMap<>();
        map.put("courseInfoVo",courseInfoVo);
        return ReturnCode.success().data(map);
    }

    /**
     *  更新课程信息
     * @param courseInfoVo
     * @return
     */
    @PostMapping("/updateCourseInfo")
    public ReturnCode updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        studyCourseService.updateCourseInfo(courseInfoVo);
        return ReturnCode.success();
    }


    /**
     * 根据课程id查询CourseIssueVo()
     * 发布课程时调用
     */
    @GetMapping("/getCourseIssueVo/{courseId}")
    public ReturnCode getCourseIssueVo(@PathVariable String courseId){
        CourseIssueVo courseIssueVo = studyCourseService.getCourseIssueInfo(courseId);
        Map<String,Object> map = new HashMap<>();
        map.put("courseIssueVo",courseIssueVo);
        return ReturnCode.success().data(map);
    }

    /**
     *  发布课程:修改状态Normal
     * @param id 课程id
     * @return
     */
    @PostMapping("/courseIssue/{id}")
    public ReturnCode courseIssue(@PathVariable String id){
        StudyCourse studyCourse = new StudyCourse();
        studyCourse.setId(id);
        studyCourse.setStatus("Normal");
        studyCourseService.updateById(studyCourse);
        return ReturnCode.success();
    }

    /**
     *  分页查询课程（有条件）
     * @param current 当前页
     * @param size 每页页数
     * @param searchCourse 查询条件
     * @return json数据
     */
    @PostMapping("/pageCourseWrapper/{current}/{size}")
    public ReturnCode pageListCourseWrapper(@ApiParam(name = "current", value = "当前页", required = true)
                                             @PathVariable Integer current,
                                             @ApiParam(name = "size", value = "每页页数", required = true)
                                             @PathVariable Integer size,
                                             @RequestBody(required = false) SearchCourse searchCourse)
    {
        //获取searchCourse对象
        String title = searchCourse.getTitle();
        String status = searchCourse.getStatus();
        String create = searchCourse.getCreate();
        String end = searchCourse.getEnd();

        //创建page
        Page<StudyCourse> pageCourse = new Page<>(current,size);
        //条件
        QueryWrapper<StudyCourse> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }
        if(!StringUtils.isEmpty(create)){
            wrapper.ge("create_time",create);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("create_time",end);
        }

        //排序方法
        wrapper.orderByDesc("create_time");

        //调用方法，底层把分页所有数据封装到pageTeacher对象中
        studyCourseService.page(pageCourse,wrapper);

        //总记录数
        long total = pageCourse.getTotal();
        //数据
        List<StudyCourse> records = pageCourse.getRecords();

        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("datas",records);

        return ReturnCode.success().data(map);
    }

    /**
     * 根据课程id删除课程
     * @param courseId 课程id
     * @return
     */
    @DeleteMapping("/deleteCourse/{courseId}")
    public ReturnCode deleteCourse(@PathVariable String courseId){
        studyCourseService.deleteCourse(courseId);
        return ReturnCode.success();
    }
}

